/*
 * ManageAuthor.java
 *
 * Created on 26 Maret 2008, 16:05
 */
package usu.perpustakaan.buku.widget.operator;

import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import usu.perpustakaan.buku.data.DefaultPublisher;
import usu.perpustakaan.buku.data.template.Publisher;
import usu.perpustakaan.buku.util.StringUtil;
import usu.perpustakaan.buku.widget.admin.ManageOperator;
import usu.perpustakaan.buku.widget.template.TableModelAuthor;
import usu.perpustakaan.buku.widget.template.TableModelPublisher;
import usu.perpustakaan.buku.widget.template.TextBoxGlass;
import usu.widget.ButtonGlass;
import usu.widget.text.DefaultDocument;
import usu.widget.util.WidgetUtilities;

/**
 *
 * @author  usu
 */
public class ManagePublisher extends javax.swing.JPanel {

    private static final long serialVersionUID = -1;
    private TableModelPublisher model;
    private DefaultPublisher publisher;
    private Publisher publisherTemp;
    private int index;
    private boolean freeze;

    /**
     * 
     * @return
     */
    public boolean isFreeze() {
        return freeze;
    }

    /**
     * 
     */
    public void reset() {
        textID.setText("");
        textName.setText("");
    }

    /**
     * 
     * @param freeze
     */
    public void setFreeze(boolean freeze) {
        this.freeze = freeze;
        textID.setEnabled(!freeze);
        textName.setEnabled(!freeze);
        textSearch.setEnabled(!freeze);
        textID.setText("");
        textName.setText("");
        textSearch.setText("");
        buttonAdd.setEnabled(!freeze);
        buttonDelete.setEnabled(!freeze);
        buttonEdit.setEnabled(!freeze);
        buttonRefresh.setEnabled(!freeze);
        table.setEnabled(!freeze);
    }

    /** Creates new form ManageAuthor */
    public ManagePublisher() {
        initVariables();
        initComponents();
        initFinal();
        initActions();
    }

    private void initActions() {
        textSearch.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                filterTable(textSearch.getText());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                filterTable(textSearch.getText());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {

                filterTable(textSearch.getText());



            }
        });



        table.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {

                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        try {
                            index = table.convertRowIndexToModel(table.getSelectedRow());
                            publisherTemp = model.get(index);
                            textID.setText(publisherTemp.getId());
                            textName.setText(publisherTemp.getName());
                        } catch (Throwable t) {
                        }
                    }
                });
    }

    /**
     * 
     * @return
     */
    public Publisher getPublisher() {
        if (textID.getText().trim().equals("") || textName.getText().trim().equals("")) {
            publisher = null;
        } else {
            publisher = new DefaultPublisher();
            publisher.setId(textID.getText());
            publisher.setName(textName.getText());
        }
        return publisher;
    }

    @SuppressWarnings("unchecked")
    private void filterTable(String teks) {
        if (table.getRowSorter() != null) {
            if (teks.trim().equals("")) {
                ((TableRowSorter<TableModelAuthor>) table.getRowSorter()).setRowFilter(null);
            } else {
                ((TableRowSorter<TableModelAuthor>) table.getRowSorter()).setRowFilter(RowFilter.regexFilter(teks));
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        usu.perpustakaan.buku.widget.template.ViewPortGlass viewPortGlass1 = new usu.perpustakaan.buku.widget.template.ViewPortGlass();
        table = new javax.swing.JTable();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        textSearch = new usu.perpustakaan.buku.widget.template.TextBoxGlass();
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        buttonDelete = new usu.widget.ButtonGlass();
        buttonEdit = new usu.widget.ButtonGlass();
        buttonAdd = new usu.widget.ButtonGlass();
        buttonRefresh = new usu.widget.ButtonGlass();
        usu.perpustakaan.buku.widget.template.PanelTranparan panelTranparan1 = new usu.perpustakaan.buku.widget.template.PanelTranparan();
        javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        textID = new usu.perpustakaan.buku.widget.template.TextBoxGlass();
        textName = new usu.perpustakaan.buku.widget.template.TextBoxGlass();

        viewPortGlass1.setView(table);

        table.setAutoCreateRowSorter(true);
        table.setForeground(new java.awt.Color(255, 255, 255));
        table.setFillsViewportHeight(true);
        table.setOpaque(false);
        table.setSelectionBackground(new java.awt.Color(51, 255, 52));
        table.setSelectionForeground(new java.awt.Color(0, 0, 0));

        setOpaque(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Cari (case sensitive) ");

        textSearch.setForeground(new java.awt.Color(255, 255, 255));
        textSearch.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textSearch.setFont(new java.awt.Font("Tahoma", 1, 11));

        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane1.setOpaque(false);
        jScrollPane1.setViewport(viewPortGlass1);

        buttonDelete.setText("Hapus");
        buttonDelete.setRoundRect(true);

        buttonEdit.setText("Ubah");
        buttonEdit.setRoundRect(true);

        buttonAdd.setText("Tambah");
        buttonAdd.setRoundRect(true);

        buttonRefresh.setText("Segarkan");
        buttonRefresh.setRoundRect(true);

        panelTranparan1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        panelTranparan1.setRound(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Id Penerbit");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Nama Penerbit");

        textID.setForeground(new java.awt.Color(255, 255, 255));
        textID.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textID.setFont(new java.awt.Font("Tahoma", 1, 11));

        textName.setForeground(new java.awt.Color(255, 255, 255));
        textName.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textName.setFont(new java.awt.Font("Tahoma", 1, 11));

        javax.swing.GroupLayout panelTranparan1Layout = new javax.swing.GroupLayout(panelTranparan1);
        panelTranparan1.setLayout(panelTranparan1Layout);
        panelTranparan1Layout.setHorizontalGroup(
            panelTranparan1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTranparan1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelTranparan1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelTranparan1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textName, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                    .addComponent(textID, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelTranparan1Layout.setVerticalGroup(
            panelTranparan1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTranparan1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelTranparan1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelTranparan1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelTranparan1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 321, Short.MAX_VALUE)
                        .addComponent(buttonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelTranparan1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    usu.widget.ButtonGlass buttonAdd;
    usu.widget.ButtonGlass buttonDelete;
    usu.widget.ButtonGlass buttonEdit;
    usu.widget.ButtonGlass buttonRefresh;
    javax.swing.JTable table;
    usu.perpustakaan.buku.widget.template.TextBoxGlass textID;
    usu.perpustakaan.buku.widget.template.TextBoxGlass textName;
    usu.perpustakaan.buku.widget.template.TextBoxGlass textSearch;
    // End of variables declaration//GEN-END:variables

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonAdd() {
        return buttonAdd;
    }

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonDelete() {
        return buttonDelete;
    }

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonEdit() {
        return buttonEdit;
    }

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonRefresh() {
        return buttonRefresh;
    }

    /**
     * 
     * @return
     */
    public JTable getTable() {
        return table;
    }

    /**
     * 
     * @return
     */
    public TextBoxGlass getTextSearch() {
        return textSearch;
    }

    /**
     * 
     * @return
     */
    public TableModelPublisher getModel() {
        return model;
    }

    private void initFinal() {
        table.setModel(model);
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellRenderer(new ManageOperator.RenderTable());
        }
        WidgetUtilities.setAutomaticPopUpMenu(table);

        textID.setDocument(new DefaultDocument(10) {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (StringUtil.containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
        textName.setDocument(new DefaultDocument(50) {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (StringUtil.containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
    }

    private void initVariables() {
        model = new TableModelPublisher();
    }
}
